package cn.cqzaier.pub.api.plugin.qianxun;

import cn.cqzaier.pub.api.Result;
import cn.cqzaier.pub.api.plugin.WxrebootCommonApiInterface;
import cn.cqzaier.pub.api.plugin.base.reqeust.qianxun.QianxunRebootRequest;
import cn.cqzaier.pub.api.plugin.base.reqeust.qianxun.QianxunRequestUtil;
import cn.cqzaier.pub.api.plugin.base.reqeust.wechat.WechatSendFileAndImage;
import cn.cqzaier.pub.api.plugin.cuteCat.response.GroupInfo;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 千寻
 */
public class WxrebootQianXunApiImpl implements WxrebootCommonApiInterface {
    private QianxunRebootRequest qianxunRebootRequest;
    private QianxunRequestUtil qianxunRequestUtil;


    @Override
    public String getRequestUrl() {
        return this.qianxunRequestUtil.getRequstUrl();
    }


    public WxrebootQianXunApiImpl(QianxunRebootRequest qianxunRebootRequest, QianxunRequestUtil qianxunRequestUtil) {
        this.qianxunRebootRequest = qianxunRebootRequest;
         this.qianxunRequestUtil  = qianxunRequestUtil;
    }

    @Override
    public Map<String, Object> getFriendlist(String rebootId) {
        getFriendOrGrouopListcommonData(rebootId, "Q0005", "3");
        return ((QianxunRequestUtil<QianxunRebootRequest, Map<String,Object>>)qianxunRequestUtil).doReq(this.qianxunRebootRequest,new Function<String, Map<String,Object>>() {
            @Override
            public Map<String,Object> apply(String s) {
                System.out.println();
                return null;
            }
        });
    }

    @Override
    public List<GroupInfo> getGrouplist(String rebootId) {
        getFriendOrGrouopListcommonData(rebootId, "Q0006", "0");
        return ((QianxunRequestUtil<QianxunRebootRequest, List<GroupInfo>>)qianxunRequestUtil).doReq(this.qianxunRebootRequest,new Function<String, List<GroupInfo>>() {
            @Override
            public List<GroupInfo> apply(String s) {
                final List<JSONObject> result = new Result<JSONObject>().getResultArry(s, JSONObject.class);
                return result.stream().map(item -> {
                    GroupInfo groupInfo = new GroupInfo();
                    groupInfo.setWxid(item.getString("wxid"));
                    groupInfo.setNickname(item.getString("nick"));
                    return groupInfo;
                }).collect(Collectors.toList());
            }
        });
    }

    private void getFriendOrGrouopListcommonData(String rebootId, String type, String value) {
        commonData(rebootId, type);
        final JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", value);
        this.qianxunRebootRequest.setData(jsonObject);
    }

    private void commonData(String rebootId, String type) {
        this.qianxunRebootRequest.setWxid(rebootId);
        this.qianxunRebootRequest.setType(type);
    }

    private void commonData(String rebootId, String type,Map<Object,Object> dataParam) {
        this.qianxunRebootRequest.setWxid(rebootId);
        this.qianxunRebootRequest.setType(type);
        this.qianxunRebootRequest.setData(JSONObject.parseObject(JSON.toJSONString(dataParam)));
    }

    private void commonDataType( String type) {
        this.qianxunRebootRequest.setType(type);
    }



    @Override
    public JSONObject sendGroupTxtMsg(String rebootId,String groupId,String msg) {
        return atGroupMemberMsgSend(rebootId, groupId, msg);
    }

    @Override
    public JSONObject sendPrivateTxtMsg(String rebootId, String groupId, String msg) {
        return sendGroupTxtMsg(rebootId,groupId,msg);
    }

    @Override
    public JSONObject sendImageMsg(String rebootId, String groupId, String pic) {
        final Map<Object, Object> param = MapUtil.builder()
                .put("wxid", groupId).put("path",pic).build();
        commonData(rebootId,"Q0010",param);
        return ((QianxunRequestUtil<QianxunRebootRequest, JSONObject>)qianxunRequestUtil).doReq(this.qianxunRebootRequest,new Function<String, JSONObject>() {
            @Override
            public JSONObject apply(String s) {
                return JSON.parseObject(s);
            }
        });
    }

    private void copyProperty(Object wechatSendImage) {
       // BeanUtils.copyProperties(this.wechatRebootRequest, wechatSendImage);
    }

    private JSONObject sendFileOrImage(WechatSendFileAndImage wechatSendImage) {
        return null;
    }


    @Override
    public JSONObject sendFile(String rebootId, String groupId, String pic) {
        return null;
    }

    @Override
    public JSONObject sendShareLink(String rebootId, String groupId, String title, String desc, String imageUrl, String url) {
        final Map<Object, Object> param = MapUtil.builder()
                .put("wxid", groupId).put("title",title)
                .put("content",desc)
                .put("jumpUrl",url)
                .put("path",imageUrl)
                .build();
        commonData(rebootId,"Q0012",param);
        return ((QianxunRequestUtil<QianxunRebootRequest, JSONObject>)qianxunRequestUtil).doReq(this.qianxunRebootRequest,new Function<String, JSONObject>() {
            @Override
            public JSONObject apply(String s) {
                return JSON.parseObject(s);
            }
        });
    }

    @Override
    public List<JSONObject> getLoggedAccountList() {
        commonDataType("Q0012");
        return ((QianxunRequestUtil<QianxunRebootRequest, List<JSONObject>>)qianxunRequestUtil).doReq(this.qianxunRebootRequest,new Function<String, List<JSONObject>>() {
            @Override
            public List<JSONObject> apply(String s) {
                return new Result<JSONObject>().getResultArry(s, JSONObject.class);
            }
        });
    }

    @Override
    public JSONObject sendGroupMsgAndAt(String rebootId, String groupId, String memberId, String msg) {
        String fromMsg = atGroupMemberStrBuild(memberId, msg);
        return atGroupMemberMsgSend(rebootId, groupId, fromMsg);
    }

    private JSONObject atGroupMemberMsgSend(String rebootId, String groupId, String fromMsg) {
        final Map<Object, Object> param = MapUtil.builder()
                .put("wxid", groupId)
                .put("msg", fromMsg).build();
        commonData(rebootId,"Q0001",param);
        return ((QianxunRequestUtil<QianxunRebootRequest, JSONObject>) qianxunRequestUtil).doReq(this.qianxunRebootRequest, new Function<String, JSONObject>() {
            @Override
            public JSONObject apply(String s) {
                return JSON.parseObject(s);
            }
        });
    }

    private String atGroupMemberStrBuild(String memberId, String msg) {
        // 消息组装
        // [@,wxid=all,nick=,isAuto=true]\n测试召唤全体\n测试[emoji=D83D][emoji=DE01]
        String fromMsg= String.format("[@,wxid=%s,nick=,isAuto=true]%s", memberId, msg);
        return fromMsg;
    }

    @Override
    public JSONObject agreeFriendVerify(String rebootId, JSONObject jsonObject) {
        // TODO
        final Map<Object, Object> param = MapUtil.builder()
                .put("scene", "")
                .put("v3", "")
                .put("v4", "")
                .build();
        commonData(rebootId,"Q0001",param);
        return ((QianxunRequestUtil<QianxunRebootRequest, JSONObject>) qianxunRequestUtil).doReq(this.qianxunRebootRequest, new Function<String, JSONObject>() {
            @Override
            public JSONObject apply(String s) {
                return JSON.parseObject(s);
            }
        });
    }

    @Override
    public JSONObject removeGroupMember(String rebootId, String groupId, String memberId, JSONObject otherInfo) {

                return null;
    }

    @Override
    public JSONObject sendVideo(String rebootId, String groupId, String videoUrl) {
        return null;
    }

    @Override
    public JSONObject sendGroupMsgAndAtAll(String rebootId, String groupId, String msg) {
        String fromMsg = atGroupMemberStrBuild("all", msg);
        return atGroupMemberMsgSend(rebootId, groupId, fromMsg);

    }

    @Override
    public JSONObject downloadFileUrlLocal(String requestUrl,String url,String path){
        return null;
    }


    @Override
    public boolean revokeMsg(String rebootId, String msgId) {
        return false;
    }
}
